// SplitDialog.cpp

#include "StdAfx.h"

#include "Common/FileSystem/FileName.h"

#ifdef LANG
#include "Common/Lang/LangUtils.h"
#endif

#include "resource.h"
#include "SplitDialog.h"
#include "Common/FileSystem/SplitUtils.h"


using namespace Common;

#ifdef LANG
static Lang::CIDLangPair kIDLangPairs[] =
{
  { IDC_STATIC_SPLIT_PATH, 0x03020501 },
  { IDC_STATIC_SPLIT_VOLUME, 0x02000D40 },
};
#endif

LRESULT CSplitDialog::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
  #ifdef LANG
  Lang::LangSetWindowText(HWND(*this), 0x03020500);
  Lang::LangSetDlgItemsText(HWND(*this), kIDLangPairs, sizeof(kIDLangPairs) / sizeof(kIDLangPairs[0]));
  #endif
  _pathCombo.Attach(GetDlgItem(IDC_COMBO_SPLIT_PATH));
  _volumeCombo.Attach(GetDlgItem(IDC_COMBO_SPLIT_VOLUME));
  
  if (!FilePath.IsEmpty())
  {
    CString title;
    GetWindowText(title);
    title += _T(' ');
    title += FilePath;
    SetWindowText(title);
  }
  _pathCombo.SetWindowText(Path);
  FileSystem::AddVolumeItems(_volumeCombo);
  _volumeCombo.SetCurSel(0);
  CenterWindow(GetParent());
  return TRUE;
}

LRESULT CSplitDialog::OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
	int xSize = LOWORD(lParam);
	int ySize = HIWORD(lParam);
  int mx, my;
  RECT rect;
  rect.left = 0;
  rect.top = 0;
  rect.right = 8;
  rect.bottom = 8;
  MapDialogRect(&rect);

  mx = rect.right - rect.left;
  my = rect.bottom - rect.top;

  int bx1, bx2, by;
  CWindow wndCancel = GetDlgItem(IDCANCEL);
  wndCancel.GetWindowRect(&rect);
  bx1 = rect.right - rect.left;
  by = rect.bottom - rect.top;
  CWindow wndOK = GetDlgItem(IDOK);
  wndOK.GetWindowRect(&rect);
  bx2 = rect.right - rect.left;
  
  int yPos = ySize - my - by;
  int xPos = xSize - mx - bx1;

  InvalidateRect(NULL);

  {
    CWindow wnd = GetDlgItem(IDC_BUTTON_SPLIT_PATH);
	wnd.GetClientRect(&rect);	  
    int bx = rect.right - rect.left;
	wnd.MoveWindow(xSize - mx - bx, rect.top, bx, rect.bottom - rect.top);
    ChangeSubWindowSizeX(_pathCombo, xSize - mx - mx - bx - mx, m_hWnd);
  }

  wndCancel.MoveWindow(xPos, yPos, bx1, by);
  wndOK.MoveWindow(xPos - mx - bx2, yPos, bx2, by); 

  
  return false;
}

LRESULT CSplitDialog::OnButtonSetPath(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  CString currentPath;
  _pathCombo.GetWindowText(currentPath);
  CString title = Lang::LangStringSpec(IDS_SET_FOLDER, 0x03020209);

  CFolderDialog dlg(*this, title, BIF_RETURNONLYFSDIRS|BIF_NEWDIALOGSTYLE| BIF_STATUSTEXT);
  if(!currentPath.IsEmpty())
	  dlg.SetInitialFolder(currentPath);
  if (IDOK == dlg.DoModal())
  {
	CString resultPath (dlg.GetFolderPath());  
	FileSystem::FileName::NormalizeDirPathPrefix(resultPath);
	_pathCombo.SetCurSel(-1);
	_pathCombo.SetWindowText(resultPath);
  }

  return 0;
}

LRESULT CSplitDialog::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  _pathCombo.GetWindowText(Path);
  CString volumeString;
  _volumeCombo.GetWindowText(volumeString);
  volumeString.Trim();
  if (!FileSystem::ParseVolumeSizes(volumeString, VolumeSizes) || VolumeSizes.size() == 0)
  {
    ::MessageBox(*this, Lang::LangString(IDS_INCORRECT_VOLUME_SIZE, 0x02000D41), _T("7-Zip"), 0);
    return 0;
  }
  EndDialog(wID);
  return 0;
}
